layui.define(['jquery', 'element','utils'], function (exports) {
	"use strict";

	var MOD_NAME = 'tab',
		$ = layui.jquery,
        utils = layui.utils,
		element = layui.element;

	var pearTab = function (opt) {
		this.option = opt;
	};

	var tabData = new Array();
	var tabDataCurrent = 0;
	var contextTabDOM;

	pearTab.prototype.render = function (opt) { // 渲染函数

		var option = {
			elem: opt.elem,
			data: opt.data,
			tool: opt.tool,
			roll: opt.roll,
			index: opt.index,
			activeRefersh: opt.activeRefersh ? opt.activeRefersh : true,
			removeOtherContent: opt.removeOtherContent ? opt.removeOtherContent : false, // 当该属性设置为true，点击时，会把其他tab的body体里的内容清掉
			removeOtherClass: opt.removeOtherClass ? opt.removeOtherClass : false, // 当该属性设置为true的时候，点击时会把其他tab下边的amis-scope类名干掉，只保留当前tab的amis-scope
			width: opt.width,
			height: opt.height,
			tabMax: opt.tabMax,
			session: opt.session ? opt.session : false, //是否开启缓存
			preload: opt.preload ? opt.preload : false,
			closeEvent: opt.closeEvent,
			success: opt.success ? opt.success : function (id) {},
			refersh: opt.refersh ? opt.refersh : function () {}
		}

		if (option.session) {
			if (sessionStorage.getItem(option.elem + "-pear-tab-data") != null) {
				tabData = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-data"));
				option.data = JSON.parse(sessionStorage.getItem(option.elem + "-pear-tab-data"));
				tabDataCurrent = sessionStorage.getItem(option.elem + "-pear-tab-data-current");
				tabData.forEach(function (item, index) {
					if (item.id == tabDataCurrent) {
						option.index = index;
					}
				})
			} else {
				tabData = opt.data;
			}
		}

		var lastIndex;
		var tab = createTab(option);

		$("#" + option.elem).html(tab);
		$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-prev").click(function () {
			rollPage("left", option);
		})
		$(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-next").click(function () {
			rollPage("right", option);
		})
		element.init();
		toolEvent(option);
		$("#" + option.elem).width(opt.width);
		$("#" + option.elem).height(opt.height);
		$("#" + option.elem).css({
			position: "relative"
		});
		closeEvent(option);

		// 如果有当前激活的id则激活当前id，否则激活第一个
		// if(sessionStorage.getItem(option.elem + "-pear-tab-data-current")){
		option.success(sessionStorage.getItem(option.elem + "-pear-tab-data-current")); // 渲染成功执行函数
		// } else {
		var id = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li[lay-id]").eq(0).attr('lay-id')
		element.tabChange(option.elem, id);
		// }
		// 渲染tab右键下拉
		$("body .layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title").on("contextmenu", "li", function (e) {
			// 获取当前元素位置
			var top = e.clientY;
			var left = e.clientX;
			var menuWidth = 100;
			var currentId = $(this).attr("lay-id");
			var menu = "<ul><li class='item' id='" + option.elem + "closeThis'>关闭当前</li><li class='item' id='" + option.elem +
				"closeOther'>关闭其他</li><li class='item' id='" + option.elem + "closeAll'>关闭所有</li><li class='item' id='" + option.elem + "refersh'>刷新页面</li></ul>";

			contextTabDOM = $(this);
			var isOutsideBounds = (left + menuWidth) > $(window).width();
			if (isOutsideBounds) {
				left = $(window).width() - menuWidth;
			}
			// 初始化
			layer.open({
				type: 1,
				title: false,
				shade: false,
				skin: 'pear-tab-menu',
				closeBtn: false,
				area: [menuWidth + 'px'],
				fixed: true,
				anim: false,
				isOutAnim: false,
				offset: [top, left],
				content: menu, //iframe的url,
				success: function (layero, index) {
					layer.close(lastIndex);
					lastIndex = index;
					menuEvent(option, index);
					var timer;
					$(layero).on('mouseout', function () {
						timer = setTimeout(function () {
							layer.close(index);
						}, 30)
					});

					$(layero).on('mouseover', function () {
						clearTimeout(timer);
					});

					// 清除 item 右击
					$(layero).on('contextmenu', function () {
						return false;
					})

				}
			});
			return false;
		})

		return new pearTab(option);
	}

	// 根据pearTab绑定的elem，寻找当前激活的div
	pearTab.prototype.getActiveContent = function (elem) {
		var div = $(elem + ' .layui-tab-content .layui-show')
		return div
	}

	// 判断 给出的id  tabs 主体是否有内容，如果有内容返回true
	pearTab.prototype.contentDomIsExist = function (elem) {
		var bool = false;
		if ($(elem).html()) {
			bool = true
		}
		return bool
	}

	pearTab.prototype.click = function (callback) { //绑定点击事件
		var elem = this.option.elem;
		var option = this.option;

		element.on('tab(' + this.option.elem + ')', function (data) {
			var id = $("#" + elem + " .layui-tab-title .layui-this").attr("lay-id");
			sessionStorage.setItem(option.elem + "-pear-tab-data-current", id);
			if (!option.preload) {
				var $iframe = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-content").find("iframe[id='" + id + "']");
				var iframeUrl = $iframe.attr("src");
				if (!iframeUrl || iframeUrl === "about:blank") {
					// 获取 url 并重载
					tabData.forEach(function (item, index) {
						if (item.id === id) {
							iframeUrl = item.url;
						}
					})
					tabIframeLoading(elem);
					$iframe.attr("src", iframeUrl);
				}
			}

            if(option.removeOtherClass){
                // console.log(id,'id')
                // console.log($('.layui-tab-item .amis-scope'),'amis-scope')
                $('.layui-tab-item .amis-scope').each(function(index,item){
                    console.log(item)
                    $(item).removeClass('amis-scope')
                })
                
                $('.layui-tab-item #treenode_'+id).addClass('amis-scope')
                // if(id === 'dashbord'){
                //     $('.layui-tab-item>div').addClass('amis-scope')
                // }
            }
            if(option.removeOtherContent){
                var contents = $('.pear-tab .layui-tab-content>.layui-tab-item')
                // console.log($('.pear-tab .layui-tab-content>.layui-tab-item'),'item')
                // console.log(id,'id')
                contents.each(function(index,item){
                    if(index>0){
                       if(!$(item).hasClass('layui-show')){
                        console.log(item,'item')
                        $(item).children().html('').attr('class','')
                       }
                    }
                })
            }
			callback(id);
		});
	}

	pearTab.prototype.positionTab = function () { // 点击和加载定位用
		var $tabTitle = $('.layui-tab[lay-filter=' + this.option.elem + ']  .layui-tab-title');
		var autoLeft = 0;
		$tabTitle.children("li").each(function () {
			if ($(this).hasClass('layui-this')) {
				return false;
			} else {
				autoLeft += $(this).outerWidth();
			}
		});
		$tabTitle.animate({
			scrollLeft: autoLeft - $tabTitle.width() / 3
		}, 200);
	}

	pearTab.prototype.clear = function () { // 清除树数据  清除当前激活的id
		sessionStorage.removeItem(this.option.elem + "-pear-tab-data");
		sessionStorage.removeItem(this.option.elem + "-pear-tab-data-current");
		sessionStorage.removeItem(this.option.elem + "-pear-current-treeNode");
	}

	pearTab.prototype.addTab = function (opt) { // 添加tab
		var title = '';
		if (opt.close) {
			title += '<span class="pear-tab-active"></span><span class="able-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>';
		} else {
			title += '<span class="pear-tab-active"></span><span class="disable-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>';
		}
		element.tabAdd(this.option.elem, {
			title: title,
			content: '<iframe id="' + opt.id + '" data-frameid="' + opt.id + '" scrolling="auto" frameborder="0" src="' +
				opt.url + '" style="width:100%;height:100%;"></iframe>',
			id: opt.id
		});
		tabData.push(opt);
		sessionStorage.setItem(this.option.elem + "-pear-tab-data", JSON.stringify(tabData));
		sessionStorage.setItem(this.option.elem + "-pear-tab-data-current", opt.id);
		element.tabChange(this.option.elem, opt.id);
	}

	var index = 0;
	// 根据过滤 fliter 标识, 重置选项卡标题
	pearTab.prototype.changeTabTitleById = function (elem, id, title) {
		var currentTab = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title [lay-id='" + id + "'] .title");
		currentTab.html(title);
	}

	// 根据过滤 filter 标识, 删除指定选项卡
	pearTab.prototype.delTabByElem = function (elem, id, callback) {
		var currentTab = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title [lay-id='" + id + "']");
		if (currentTab.find("span").is(".able-close")) {
			tabDelete(elem, id, callback);
		}
	}
	// 根据过滤 filter 标识, 删除其他选项卡
	pearTab.prototype.delOtherTabByElem = function (elem, callback) {
		var currentId = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title .layui-this").attr("lay-id");
		var tabtitle = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li");
		$.each(tabtitle, function (i) {
			if ($(this).attr("lay-id") != currentId) {
				if ($(this).find("span").is(".able-close")) {
					tabDelete(elem, $(this).attr("lay-id"), callback);
				}
			}
		})
	}

	// 根据过滤 filter 标识, 删除全部选项卡
	pearTab.prototype.delAllTabByElem = function (elem, callback) {
		var currentId = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title .layui-this").attr("lay-id");
		var tabtitle = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li");
		$.each(tabtitle, function (i) {
			if ($(this).find("span").is(".able-close")) {
				tabDelete(elem, $(this).attr("lay-id"), callback);
			}
		})
	}
	// 根据过滤 filter 标识, 删除当前选项卡
	pearTab.prototype.delCurrentTabByElem = function (elem, callback) {
		var currentTab = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title .layui-this");
		if (currentTab.find("span").is(".able-close")) {
			var currentId = currentTab.attr("lay-id");
			tabDelete(elem, currentId, callback);
		}
	}
	// 通过过滤 filter 标识, 新增标签页
	pearTab.prototype.addTabOnlyByElem = function (elem, opt, time) {

		var title = '';
		if (opt.close) {
			title += '<span class="pear-tab-active"></span><span class="able-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>'
		} else {
			title += '<span class="pear-tab-active"></span><span class="disable-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>'
		}
		if ($(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li[lay-id]").length <= 0) {
			element.tabAdd(elem, {
				title: title,
				content: '<iframe id="' + opt.id + '" data-frameid="' + opt.id + '" scrolling="auto" frameborder="0" src="' +
					opt.url + '" style="width:100%;height:100%;"></iframe>',
				id: opt.id
			});
			if (time != false && time != 0) {
				tabIframeLoading(elem, opt.id);
			}
			tabData.push(opt);
			sessionStorage.setItem(elem + "-pear-tab-data", JSON.stringify(tabData));
		} else {
			var isData = false;
			$.each($(".layui-tab[lay-filter='" + elem + "'] .layui-tab-title li[lay-id]"), function () {
				if ($(this).attr("lay-id") == opt.id) {
					isData = true;
				}
			})

			if (isData == false) {
				element.tabAdd(elem, {
					title: title,
					content: '<iframe id="' + opt.id + '" data-frameid="' + opt.id + '" scrolling="auto" frameborder="0" src="' +
						opt.url + '" style="width:100%;height:100%;"></iframe>',
					id: opt.id
				});
				if (time != false && time != 0) {
					tabIframeLoading(this.option.elem, opt.id);
				}
				tabData.push(opt);
				sessionStorage.setItem(elem + "-pear-tab-data", JSON.stringify(tabData));

			}
		}
		sessionStorage.setItem(elem + "-pear-tab-data-current", opt.id);
		element.tabChange(elem, opt.id);
	}

	/** 添 加 唯 一 选 项 卡 */
	pearTab.prototype.addTabOnly = function (opt, callback, time) {

		var title = '';
		if (opt.close) {
			title += '<span class="pear-tab-active"></span><span class="able-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>';
		} else {
			title += '<span class="pear-tab-active"></span><span class="disable-close title">' + opt.title +
				'</span><i class="layui-icon layui-unselect layui-tab-close">ဆ</i>';
		}
		var content;
		if (opt.mode !== 'dom') {
			content = '<iframe id="' + opt.id + '" data-frameid="' + opt.id + '" scrolling="auto" frameborder="0" src="' +
				opt.url + '" style="width:100%;height:100%;"></iframe>'
		} else {
			content = opt.content
		}
		if ($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]").length <= 0) { //还没有tab
			element.tabAdd(this.option.elem, {
				title: title,
				content: content,
				id: opt.id
			});
			if (time != false && time != 0) {
				tabIframeLoading(this.option.elem, opt.id);
			}
			tabData.push(opt);
			sessionStorage.setItem(this.option.elem + "-pear-tab-data", JSON.stringify(tabData));
			sessionStorage.setItem(this.option.elem + "-pear-tab-data-current", opt.id);
		} else {
			var isData = false;
			$.each($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]"), function () {
				if ($(this).attr("lay-id") == opt.id) {
					isData = true;
				}
			})
			if (isData == false) {

				if (this.option.tabMax != false) {
					if ($(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-title li[lay-id]").length >= this.option.tabMax) {
						layer.msg("最多打开" + this.option.tabMax + "个标签页", {
							icon: 2,
							time: 1000,
							shift: 6
						});
						return false;
					}
				}
				element.tabAdd(this.option.elem, {
					title: title,
					content: content,
					id: opt.id
				});
				if (time != false && time != 0) {
					tabIframeLoading(this.option.elem, opt.id);
				}
				tabData.push(opt);
				sessionStorage.setItem(this.option.elem + "-pear-tab-data", JSON.stringify(tabData));
				sessionStorage.setItem(this.option.elem + "-pear-tab-data-current", opt.id);
			} else {
				var id = opt.id;
				// if(!this.option.activeRefersh === false){
				//     this.delTabByElem(this.option.elem, opt.id, function(id){})
				//     element.tabAdd(this.option.elem, {
				//         title: title,
				//         content: content,
				//         id: opt.id
				//     });
				//     // this.click(function(){})
				//     // element.tabChange(this.option.elem, opt.id);
				// }

			}
		}
		element.tabChange(this.option.elem, opt.id);
		this.positionTab()
		// this.refresh(this.option.id)
		if (callback) {
			callback()
		}
		sessionStorage.setItem(this.option.elem + "-pear-tab-data-current", opt.id);
	}

	// 刷 新 指 定 的 选 项 卡
	pearTab.prototype.refresh = function (time) {
		// 刷 新 指 定 的 选 项 卡
		var $iframe = $(".layui-tab[lay-filter='" + this.option.elem + "'] .layui-tab-content .layui-show").find("iframe");
		if (time != false && time != 0) {
			tabIframeLoading(this.option.elem);
		}
		$iframe.attr("src", $iframe.attr("src"));
	}

	function tabIframeLoading(elem, id) {
		var load = '<div id="pear-tab-loading' + index + '" class="pear-tab-loading">' +
			'<div class="ball-loader">' +
			'<span></span><span></span><span></span><span></span>' +
			'</div>' +
			'</div>'
		var $iframe = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-content .layui-show").find("iframe");
		if (id) {
			$iframe = $(".layui-tab[lay-filter='" + elem + "'] .layui-tab-content").find("iframe[id='" + id + "']");
		}
		$iframe.parent().append(load);
		var pearLoad = $("#" + elem).find("#pear-tab-loading" + index);
		pearLoad.css({
			display: "block"
		});
		index++;
		$iframe.on("load", function () {
			pearLoad.fadeOut(1000, function () {
				pearLoad.remove();
			});
		})
	}


	function tabDelete(elem, id, callback, option) { // 根据id删除相对应的tab和内容

		//根据 elem id 来删除指定的 layui title li
		var tabTitle = $(".layui-tab[lay-filter='" + elem + "']").find(".layui-tab-title");
		// 删除指定 id 的 title
		var removeTab = tabTitle.find("li[lay-id='" + id + "']");
		var nextNode = removeTab.next("li");
		if (!removeTab.hasClass("layui-this")) {
			removeTab.remove();
			var tabContent = $(".layui-tab[lay-filter='" + elem + "']").find("div[id='treenode_" + id + "']").parent();
			tabContent.remove();

			tabData = JSON.parse(sessionStorage.getItem(elem + "-pear-tab-data"));
			tabDataCurrent = sessionStorage.getItem(elem + "-pear-tab-data-current");
			tabData = tabData.filter(function (item) {
				return item.id != id;
			})
			sessionStorage.setItem(elem + "-pear-tab-data", JSON.stringify(tabData));
			return false;
		}

		// 关闭当前tab，如果后边有tab，激活后边的tab
		var currId;
		if (nextNode.length) {
			nextNode.addClass("layui-this");
			currId = nextNode.attr("lay-id");
			$("#" + elem + " [id='treenode_" + currId + "']").parent().addClass("layui-show");
		} else {
			var prevNode = removeTab.prev("li");
			prevNode.addClass("layui-this");
			currId = prevNode.attr("lay-id");
			console.log(currId, 'currId')
			if (currId === 'dashbord') {
				$("#" + elem + " .layui-tab-content>.layui-tab-item").addClass("layui-show");
			}else {
				$("#" + elem + " [id='treenode_" + currId + "']").parent().addClass("layui-show");
			}

		}
		// 判断是不是amis页面，如果是的话，添加amis-scope类名
		if ($("#" + elem + " [id='treenode_" + currId + "']").find('.amis-routes-wrapper').length > 0) {
			$("#" + elem + " [id='treenode_" + currId + "']").addClass('amis-scope')
		}
		if (callback) {
			callback(currId);
		}
		tabData = JSON.parse(sessionStorage.getItem(elem + "-pear-tab-data"));
		tabDataCurrent = sessionStorage.getItem(elem + "-pear-tab-data-current");
		tabData = tabData.filter(function (item) {
			return item.id != id;
		})
		sessionStorage.setItem(elem + "-pear-tab-data", JSON.stringify(tabData));
		sessionStorage.setItem(elem + "-pear-tab-data-current", currId);
        utils.setTreeNode(currId)
        // 刷新页面

		if(option){
			option.refersh(currId);
		}
		removeTab.remove();
		// 删除 content
		var tabContent = $(".layui-tab[lay-filter='" + elem + "']").find("div[id='treenode_" + id + "']").parent();;
		// 删除
		tabContent.remove();
	}



	function createTab(option) { // 生成tabs和内容，采取何种渲染模式在这里传进来 option
		var type = "";
		var types = option.type + " ";
		if (option.roll == true) {
			type = "layui-tab-roll";
		}
		if (option.tool != false) {
			type = "layui-tab-tool";
		}
		if (option.roll == true && option.tool != false) {
			type = "layui-tab-rollTool";
		}

		var tab = '<div class="pear-tab ' + types + type + ' layui-tab" lay-filter="' + option.elem +
			'" lay-allowClose="true">';
		var title = '<ul class="layui-tab-title changeXScroll" overflow-x="auto" id="XScroll">';
		$("#XScroll").click(function () {
			// alert("你好");
		});
		var content = '<div class="layui-tab-content">';
		var control =
			'<div class="layui-tab-control"><li class="layui-tab-prev layui-icon layui-icon-left"></li><li class="layui-tab-next layui-icon layui-icon-right"></li><li class="layui-tab-tool layui-icon layui-icon-down"><ul class="layui-nav" lay-filter=""><li class="layui-nav-item"><a href="javascript:;"></a><dl class="layui-nav-child">';
		// var control =
		// 	'<div class="layui-tab-control"><li class="layui-tab-tool layui-icon layui-icon-down"><ul class="layui-nav" lay-filter=""><li class="layui-nav-item"><a href="javascript:;"></a><dl class="layui-nav-child">';



		// 处 理 选 项 卡 头 部
		var index = 0;
		$.each(option.data, function (i, item) {
			var TitleItem = '';
			// 判断激活的是否是当前
			if (option.index == index) {
				TitleItem += '<li lay-id="' + item.id + '" class="layui-this"><span class="pear-tab-active"></span>';
			} else {
				TitleItem += '<li lay-id="' + item.id + '" ><span class="pear-tab-active"></span>';
			}

			if (item.close) {
				// 当 前 选 项 卡 可 以 关 闭
				TitleItem += '<span class="able-close title">' + item.title + '</span>';
			} else {
				// 当 前 选 项 卡 不 允 许 关 闭
				TitleItem += '<span class="disable-close title">' + item.title + '</span>';
			}
			TitleItem += '<i class="layui-icon layui-unselect layui-tab-close">ဆ</i></li>';
			title += TitleItem;
			// 这里值提供了一种渲染模式   iframe的渲染模式
			if (option.index == index) {
				// 处 理 显 示 内 容
				if (item.mode === 'dom') {
					content += '<div class="layui-tab-item">' + item.domContent + '</div>'
				} else {
					content += '<div class="layui-show layui-tab-item"><iframe id="' + item.id + '" data-frameid="' + item.id +
						'"  src="' + item.url +
						'" frameborder="no" border="0" marginwidth="0" marginheight="0" style="width: 100%;height: 100%;"></iframe></div>'
				}


			} else {
				if (!option.preload) {
					item.url = "about:blank";
				}
				// 处 理 显 示 内 容
				if (item.mode === 'dom') {
					content += '<div class="layui-tab-item">' + item.domContent + '</div>'
				} else {
					content += '<div class="layui-tab-item"><iframe id="' + item.id + '" data-frameid="' + item.id + '"  src="' +
						item.url +
						'" frameborder="no" border="0" marginwidth="0" marginheight="0" style="width: 100%;height: 100%;"></iframe></div>'
				}

			}
			index++;
		});

		title += '</ul>';
		content += '</div>';
		control += '<dd id="closeThis"><a href="#">关 闭 当 前</a></dd>'
		control += '<dd id="closeOther"><a href="#">关 闭 其 他</a></dd>'
		control += '<dd id="closeAll"><a href="#">关 闭 全 部</a></dd>'
		control += '<dd id="refersh"><a href="#">刷 新 页 面</a></dd>'
		control += '</dl></li></ul></li></div>';

		tab += title;
		tab += control;
		tab += content;
		tab += '</div>';
		tab += ''

		return tab;
	}




	function rollPage(d, option) {
		var $tabTitle = $('#' + option.elem + '  .layui-tab-title');
		var left = $tabTitle.scrollLeft();
		if ('left' === d) {
			$tabTitle.animate({
				scrollLeft: left - 450
			}, 200);
		} else {
			$tabTitle.animate({
				scrollLeft: left + 450
			}, 200);
		}
	}

	function closeEvent(option) { //关闭的时候调用函数
		$(".layui-tab[lay-filter='" + option.elem + "']").on("click", ".layui-tab-close", function () {
			var layid = $(this).parent().attr("lay-id");
			tabDelete(option.elem, layid, option.closeEvent, option);
		})
	}

	function menuEvent(option, index) { // 右击title

		$("#" + option.elem + "closeThis").click(function () {
			var currentTab = contextTabDOM;

			if (currentTab.find("span").is(".able-close")) {
				var currentId = currentTab.attr("lay-id");
				tabDelete(option.elem, currentId, option.closeEvent, option);
			} else {
				layer.msg("当前页面不允许关闭", {
					icon: 3,
					time: 800
				})
			}
			layer.close(index);
		})

		$("#" + option.elem + "closeOther").click(function () {
			var currentId = contextTabDOM.attr("lay-id");
			var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
			$.each(tabtitle, function (i) {
				if ($(this).attr("lay-id") != currentId) {
					if ($(this).find("span").is(".able-close")) {
						tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent, option);
					}
				}
			})
			layer.close(index);
		})

		$("#" + option.elem + "closeAll").click(function () {
			var currentId = contextTabDOM.attr("lay-id");
			var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
			$.each(tabtitle, function (i) {
				if ($(this).find("span").is(".able-close")) {
					tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent, option);
				}
			})
			layer.close(index);
		})

		$("#" + option.elem + "refersh").click(function () {
			var currentId = contextTabDOM.attr("lay-id"); // 获取右键id
			option.refersh(currentId)
			layer.close(index);
		})
	}


	function toolEvent(option) { // 向下箭头

		$("body .layui-tab[lay-filter='" + option.elem + "']").on("click", "#closeThis", function () {
			var currentTab = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title .layui-this");
			if (currentTab.find("span").is(".able-close")) {
				var currentId = currentTab.attr("lay-id");
				tabDelete(option.elem, currentId, option.closeEvent, option);
			} else {
				layer.msg("当前页面不允许关闭", {
					icon: 3,
					time: 800
				})
			}
		})

		$("body .layui-tab[lay-filter='" + option.elem + "']").on("click", "#closeOther", function () {
			var currentId = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title .layui-this").attr("lay-id");
			var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
			$.each(tabtitle, function (i) {
				if ($(this).attr("lay-id") != currentId) {
					if ($(this).find("span").is(".able-close")) {
						tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent, option);
					}
				}
			})
		})

		$("body .layui-tab[lay-filter='" + option.elem + "']").on("click", "#closeAll", function () {
			var currentId = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title .layui-this").attr("lay-id");
			var tabtitle = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title li");
			$.each(tabtitle, function (i) {
				if ($(this).find("span").is(".able-close")) {
					tabDelete(option.elem, $(this).attr("lay-id"), option.closeEvent, option);
				}
			})
		})

		$("body .layui-tab[lay-filter='" + option.elem + "']").on("click", "#refersh", function () {
			var currentId = $(".layui-tab[lay-filter='" + option.elem + "'] .layui-tab-title .layui-this").attr("lay-id");
			option.refersh(currentId)
		})
	}

	exports(MOD_NAME, new pearTab());
})